<?php


namespace app\service;


use app\entity\ActListEntity;
use app\entity\ActTypeEntity;
use app\entity\GamesEntity;
use app\exception\ErrorCode;
use app\exception\OaException;
use app\models\request\ReqActListPage;
use app\models\request\ReqApplyAct;
use app\models\response\RespPage;
use yii\data\Pagination;


class ActService
{

    /**
     * @return array|\yii\db\ActiveRecord[]|ActTypeEntity[]
     */
    public function getTypeList(){
        return ActTypeEntity::find()->where(["del_flag"=>0])->all();
    }
    public function getGameList(){
        return GamesEntity::find()->where(["del_flag"=>0])->all();
    }

    /**
     * @param $id
     * @return array|\yii\db\ActiveRecord|ActListEntity
     * @throws OaException
     */
    public function findByPk($id){
        $entity = ActListEntity::find()->where(["del_flag"=>0,"id"=>$id])->one();
        if (!$entity)    throw new OaException(ErrorCode::act_not_found);
        return $entity;
    }


   public function actDetail($id){
       $entity = ActListEntity::find()->where(["del_flag"=>0,"id"=>$id])->one();
       if (!$entity)    throw new OaException(ErrorCode::act_not_found);

       $status=0;
       if (strtotime($entity->getBeginTime())  < time()  && strtotime($entity->getEndTime())  > time())
           $status=1;
       if (strtotime($entity->getEndTime())  < time())
           $status=2;

       return [
           "id"=>$entity->getId(),
           "title"=>$entity->getTitle(),
           "beginTime"=>$entity->getBeginTime(),
           "endTime"=>$entity->getEndTime(),
           "applyBeginTime"=>$entity->getApplyBeginTime(),
           "applyEndTime"=>$entity->getApplyEndTime(),
           "picUrl"=>$entity->getPicUrl(),
           "act_rule"=>$entity->getActRule(),
           "real_material"=>json_decode($entity->getRealMaterial(),true),
           "status"=>$status,
           "train_material"=>$entity->getTrainMaterial(),
           "feedback_material"=>$entity->getFeedbackMaterial()
       ];
   }


    /**
     * @param ReqActListPage $req
     * @return RespPage
     */
    public function listPage(ReqActListPage  $req) {
        $data = ActListEntity::find()->where(["del_flag"=>0])->orderBy("id desc");
        if ($req->getGameId()) {
            $data->andWhere(["game_id"=>$req->getGameId()]);
        }
        $time = date("Y-m-d G:i:s");
        if ($req->getStatus() == 1) {
            $data->andWhere("begin_time < '".$time."' AND end_time > '".$time."'");
        }

        if ($req->getStatus() == 2) {
            $data->andWhere(" end_time < '".$time."'");
        }

        $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $req->getPageSize(),'page'=>$req->getPageNo()-1]);
        /**@var $entities ActListEntity[]*/
        $entities = $data->offset($pages->getOffset())->limit($pages->getLimit())->all();
        $list = [];
        foreach ($entities as $entity) {
            $status=0;
            if (strtotime($entity->getApplyBeginTime())  < time()  && strtotime($entity->getApplyEndTime())  > time())
                $status=1;
            if (strtotime($entity->getApplyEndTime())  < time())
                $status=2;
            array_push($list,[
                "id"=>$entity->getId(),
                "title"=>$entity->getTitle(),
                "beginTime"=>$entity->getBeginTime(),
                "endTime"=>$entity->getEndTime(),
                "applyBeginTime"=>$entity->getApplyBeginTime(),
                "applyEndTime"=>$entity->getApplyEndTime(),
                "picUrl"=>$entity->getPicUrl(),
                "act_rule"=>$entity->getActRule(),
                "material_info"=>json_decode($entity->getMaterialInfo(),true),
                "real_material"=>json_decode($entity->getRealMaterial(),true),
                "status"=>$status,
                "train_material"=>$entity->getTrainMaterial(),
                "feedback_material"=>$entity->getFeedbackMaterial()
            ]);
        }

        $resp = new RespPage();
        $resp->setTotal(intval($pages->totalCount));
        $resp->setPageSize($pages->getPageSize());
        $resp->setPageNo($req->getPageNo());
        $resp->setList($list);
        return $resp;
    }





}